﻿Imports System.IO

Public Class Form1
    Dim NameList As New ArrayList
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        NameList.Clear()
        Dim OpenFile As New OpenFileDialog
        OpenFile.Filter = ".txt|*.txt"

        Dim result As DialogResult = OpenFile.ShowDialog()
        Try
            Dim path As String = OpenFile.FileName
            ' Read in text.
            Dim text As String = File.ReadAllText(path)
            Dim listtext As String() = text.Split(vbCrLf)
            For Each item In listtext
                NameList.Add(item)
            Next
            ' For debugging.
            'Me.Text = text.Length.ToString
        Catch ex As Exception
            ' Report an error.
            'Me.Text = "Error"
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Dim RandomSecondsCount As Integer = 12
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If NameList.Count <> 0 Then
            Timer1.Enabled = True
            Dim Rando As New Random
            RandomSecondsCount = Rando.Next(7, 15)
            PictureBox1.Visible = False
            PictureBox2.Visible = False
            Timer3.Enabled = False
            'Label1.ForeColor = Color.Black
            TimerWatch = 0
        Else
            MsgBox("No Names in the list", MsgBoxStyle.Critical)
        End If
    End Sub
    Dim Excluded As New ArrayList
    Function PickRandom() As String
SelectAgain:
        If Excluded.Count = NameList.Count Then
            'MsgBox("All Names has been called")
            Label1.Text = "All Names has been called"
            Timer1.Enabled = False
            Timer2.Enabled = False
            Timer3.Enabled = False
            Return Nothing
        End If
        Dim rand As New Random
        Dim ran As Integer = rand.Next(0, NameList.Count)
        Dim ReturnString As String = NameList(ran).ToString.Replace(Chr(10), "")
        For Each item In Excluded
            If ReturnString = item Then
                GoTo SelectAgain
            End If
        Next

        'Excluded.Add(ReturnString)
        Return ReturnString
    End Function
    Dim TimerWatch As Integer = 0
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label1.Text = PickRandom()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        TimerWatch += 1
        Timer2.Enabled = True
        Timer2.Interval = TimerWatch * 50
        'Win!
        Try
            If TimerWatch = RandomSecondsCount Then
                Excluded.Add(Label1.Text)
                Timer1.Enabled = False
                Timer2.Enabled = False
                PictureBox1.Visible = True
                PictureBox2.Visible = True
                Timer3.Enabled = True
                AddExcludedonTextFile(Label1.Text)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    '    If Label1.ForeColor = Color.Red Then
    '        Label1.ForeColor = Color.Black
    '    Else
    '        Label1.ForeColor = Color.Red
    '    End If
    End Sub
    Sub AddExcludedonTextFile(Name)
        Try
            Dim FILE_NAME As String = My.Application.Info.DirectoryPath & "\excluded.txt"
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
            objWriter.WriteLine(Name)
            objWriter.Close()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If File.Exists(My.Application.Info.DirectoryPath & "\excluded.txt") Then
            Dim text As String = File.ReadAllText(My.Application.Info.DirectoryPath & "\excluded.txt")
            Dim listtext As String() = text.Split(vbCrLf)
            For Each item In listtext
                Excluded.Add(item.ToString.Replace(Chr(10), ""))
            Next
        End If
        'Label1.BackColor = Color.Transparent
        'PictureBox1 
    End Sub
End Class
